home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / GNU / emacs.inst / emacs19.idb / usr / gnu / lib / emacs / site-lisp / mouse-sel.el.z / mouse-sel.el
Encoding:
Text File  |  1994-08-02  |  15.6 KB  |  449 lines

  1. ;;; mouse-sel.el --- Multi-click selection support for Emacs 19
  2.  
  3. ;; Copyright (C) Mike Williams 1993
  4.  
  5. ;; Author: Mike Williams <mikew@gopher.dosli.govt.nz>
  6. ;; Keywords: mouse
  7. ;; Version: $Revision: 1.20 $
  8.  
  9. ;; This file is not (yet) part of GNU Emacs, but is made available under
  10. ;; the same conditions.
  11. ;;
  12. ;; GNU Emacs is free software; you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 2, or (at your option)
  15. ;; any later version.
  16. ;;
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. ;; GNU General Public License for more details.
  21.  
  22. ;;; Commentary ============================================================
  23. ;;
  24. ;; This module provides multi-click mouse support for GNU Emacs versions
  25. ;; 19.18 and later.  I've tried to make it behave more like standard X
  26. ;; clients (eg. xterm) than the default Emacs 19 mouse selection handlers.
  27. ;; Basically:
  28. ;;
  29. ;;   * Clicking mouse-1 starts (cancels) selection, dragging extends it.
  30. ;;
  31. ;;   * Clicking or dragging mouse-3 extends the selection as well.
  32. ;;
  33. ;;   * Double-clicking on word constituents selects words.
  34. ;;     Double-clicking on symbol constituents selects symbols.
  35. ;;     Double-clicking on quotes or parentheses selects sexps.
  36. ;;     Double-clicking on whitespace selects whitespace.
  37. ;;     Triple-clicking selects lines.
  38. ;;
  39. ;;   * Selecting sets the region & X primary selection, but does NOT affect
  40. ;;     the kill-ring.  Because the mouse handlers set the primary selection
  41. ;;     directly, mouse-sel sets the variables interprogram-cut-function
  42. ;;     and interprogram-paste-function to nil.
  43. ;;
  44. ;;   * Clicking mouse-2 pastes contents of primary selection.
  45. ;;
  46. ;;   * Pressing mouse-2 while selecting or extending copies selected text
  47. ;;     to the kill ring.  Pressing mouse-1 or mouse-3 kills it.
  48. ;;
  49. ;; This module requires my thing@pt.el module, version 1.14 or later, which
  50. ;; it uses to find the bounds of words, lines, sexps, etc.  You should be
  51. ;; able to find thing@pt.el in the same place you found mouse-sel.el.
  52. ;;
  53. ;; Thanks to KevinB@bartley.demon.co.uk for his useful input.
  54. ;;
  55. ;;--- Installation --------------------------------------------------------
  56. ;;
  57. ;; 1. Put mouse-sel.el & thing@pt.el somewhere on your load path.
  58. ;;    Byte-compile them for improved speed.
  59. ;;
  60. ;; 2. Place the following in your .emacs:
  61. ;;
  62. ;;      (require 'mouse-sel)
  63. ;;
  64. ;;    You may also want to add none or more of following:
  65. ;;
  66. ;;      ;; Enable region highlight
  67. ;;      (transient-mark-mode 1)
  68. ;;
  69. ;;      ;; But only in the selected window
  70. ;;      (setq highlight-nonselected-windows nil)
  71. ;;      
  72. ;;      ;; Enable pending-delete
  73. ;;      (delete-selection-mode 1)
  74. ;;
  75. ;;--- Customisation -------------------------------------------------------
  76. ;;
  77. ;; * You can control the way mouse-sel binds it's keys by setting the value
  78. ;;   of mouse-sel-default-bindings before loading mouse-sel.
  79. ;;
  80. ;;   (a) If mouse-sel-default-bindings = t (the default)
  81. ;;   
  82. ;;       Mouse sets and pastes selection
  83. ;;       mouse-1        mouse-select
  84. ;;       mouse-2        mouse-insert-selection
  85. ;;         mouse-3        mouse-extend
  86. ;;
  87. ;;       Selection/kill-ring interaction is disabled
  88. ;;         interprogram-cut-function   = nil
  89. ;;         interprogram-paste-function = nil
  90. ;;
  91. ;;   (b) If mouse-sel-default-bindings = 'interprogram-cut-paste
  92. ;;   
  93. ;;       Mouse sets selection, and pastes from kill-ring
  94. ;;       mouse-1        mouse-select
  95. ;;       mouse-2        mouse-yank-at-click
  96. ;;       mouse-3        mouse-extend
  97. ;;  
  98. ;;       Selection/kill-ring interaction is retained
  99. ;;         interprogram-cut-function   = x-select-text
  100. ;;         interprogram-paste-function = x-cut-buffer-or-selection-value
  101. ;;         
  102. ;;       What you lose is the ability to select some text in
  103. ;;       delete-selection-mode and yank over the top of it.
  104. ;;       
  105. ;;   (c) If mouse-sel-default-bindings = nil, no bindings are made.
  106. ;;
  107. ;; * I like to leave point at the end of the region nearest to where the
  108. ;;   mouse was, even though this makes region highlighting mis-leading (the
  109. ;;   cursor makes it look like one extra character is selected).  You can
  110. ;;   disable this behaviour with:
  111. ;;
  112. ;;     (setq mouse-sel-leave-point-near-mouse nil)
  113. ;;
  114. ;; * Normally, the selection highlight will be removed when the mouse is
  115. ;;   lifted.  You can tell mouse-sel to retain the selection highlight
  116. ;;   (useful if you don't use transient-mark-mode) with:
  117. ;;
  118. ;;     (setq mouse-sel-retain-highlight t)
  119. ;;
  120. ;; * By default, mouse-select cycles the click count after 3 clicks.  That
  121. ;;   is, clicking mouse-1 four times has the same effect as clicking it
  122. ;;   once, clicking five times has the same effect as clicking twice, etc.
  123. ;;   Disable this behaviour with:
  124. ;;
  125. ;;     (setq mouse-sel-cycle-clicks nil)
  126. ;;
  127. ;; * The variables mouse-sel-{set,get,check}-selection-function control how
  128. ;;   the selection is handled.  Under X Windows, these variables default so
  129. ;;   that the X primary selection is used.  Under other windowing systems,
  130. ;;   alternate functions are used, which simply store the selection value
  131. ;;   in a variable.
  132. ;;
  133. ;;--- Hints ---------------------------------------------------------------
  134. ;;
  135. ;; * You can change the selection highlight face by altering the properties
  136. ;;   of mouse-drag-overlay, eg.
  137. ;;
  138. ;;     (overlay-put mouse-drag-overlay 'face 'bold)
  139. ;;
  140. ;; * Pasting from the primary selection under emacs 19.19 is SLOW (there's
  141. ;;   a two second delay).  The following code will cause mouse-sel to use
  142. ;;   the cut buffer rather than the primary selection.  However, be aware
  143. ;;   that cut buffers are OBSOLETE, and some X applications may not support
  144. ;;   them.
  145. ;;   
  146. ;;     (setq mouse-sel-set-selection-function 'x-select-text
  147. ;;           mouse-sel-get-selection-function 'x-get-cut-buffer)
  148. ;;           
  149. ;;--- Warnings ------------------------------------------------------------
  150. ;;
  151. ;; * When selecting sexps, the selection extends by sexps at the same
  152. ;;   nesting level.  This also means the selection cannot be extended out
  153. ;;   of the enclosing nesting level.  This is INTENTIONAL.
  154.  
  155. ;;; Code ==================================================================
  156.  
  157. (provide 'mouse-sel)
  158.  
  159. (require 'mouse)
  160. (require 'thing@pt)
  161.  
  162. ;;=== Version =============================================================
  163.  
  164. (defconst mouse-sel-version (substring "$Revision: 1.20 $" 11 -2)
  165.   "The revision number of mouse-sel (as string).  The complete RCS id is:
  166.  
  167.   $Id: mouse-sel.el,v 1.20 1993/09/30 23:57:32 mike Exp $")
  168.  
  169. ;;=== User Variables ======================================================
  170.  
  171. (defvar mouse-sel-leave-point-near-mouse t
  172.   "*Leave point near last mouse position.
  173. If non-nil, \\[mouse-select] and \\[mouse-extend] will leave point at the end
  174. of the region nearest to where the mouse last was.
  175. If nil, point will always be placed at the beginning of the region.")
  176.  
  177. (defvar mouse-sel-retain-highlight nil
  178.   "*Retain highlight on mouse-drag-overlay.
  179. If non-nil, regions selected using \\[mouse-select] and \\[mouse-extend] will
  180. remain highlighted.
  181. If nil, highlighting will be turned off when the mouse is lifted.")
  182.  
  183. (defvar mouse-sel-cycle-clicks t
  184.   "*If non-nil, \\[mouse-select] cycles the click-counts after 3 clicks.
  185. Ie. 4 clicks = 1 click, 5 clicks = 2 clicks, etc.")
  186.  
  187. (defvar mouse-sel-default-bindings t
  188.   "Set to nil before loading mouse-sel to prevent default mouse bindings.")
  189.  
  190. ;;=== Selection ===========================================================
  191.  
  192. (defvar mouse-sel-selection-type nil "Type of current selection")
  193. (make-variable-buffer-local 'mouse-sel-selection-type)
  194.  
  195. (defvar mouse-sel-selection "" 
  196.   "This variable is used to store the selection value when mouse-sel is
  197. used on windowing systems other than X Windows.")
  198.  
  199. (defvar mouse-sel-set-selection-function 
  200.   (if (eq window-system 'x) 
  201.       (function (lambda (s) (x-set-selection 'PRIMARY s)))
  202.     (function (lambda (s) (setq mouse-sel-selection s))))
  203.   "Function to call to set selection.
  204. Called with one argument, the text to select.")
  205.  
  206. (defvar mouse-sel-get-selection-function
  207.   (if (eq window-system 'x) 
  208.       'x-get-selection 
  209.     (function (lambda () mouse-sel-selection)))
  210.   "Function to call to get the selection.
  211. Called with no argument, it should return the selected text.")
  212.  
  213. (defvar mouse-sel-check-selection-function
  214.   (if (eq window-system 'x) 
  215.       'x-selection-owner-p 
  216.     nil)
  217.   "Function to check whether emacs still owns the selection.
  218. Called with no arguments.")
  219.  
  220. (defun mouse-sel-determine-selection-type (NCLICKS)
  221.   "Determine what `thing' \\[mouse-select] and \\[mouse-extend] should
  222. select by.  The first argument is NCLICKS, is the number of consecutive
  223. mouse clicks at the same position."
  224.   (let* ((next-char (char-after (point)))
  225.      (char-syntax (if next-char (char-syntax next-char)))
  226.      (nclicks (if mouse-sel-cycle-clicks (1+ (% (1- NCLICKS) 3)) NCLICKS)))
  227.     (cond
  228.      ((= nclicks 1) nil)
  229.      ((>= nclicks 3) 'line)
  230.      ((memq char-syntax '(?\( ?\) ?\" ?')) 'sexp)
  231.      ((memq next-char '(? ?\t ?\n)) 'whitespace)
  232.      ((eq char-syntax ?_) 'symbol)
  233.      ((eq char-syntax ?w) 'word))))
  234.  
  235. (defun mouse-select (EVENT)
  236.   "Set region/selection using the mouse.
  237.  
  238. On click, point & mark are set to click position, and mark is disabled.
  239. Dragging extends region/selection.
  240.  
  241. Double-clicking on word constituents selects words.
  242. Double-clicking on symbol constituents selects symbols.
  243. Double-clicking on quotes or parentheses selects sexps.
  244. Double-clicking on whitespace selects whitespace.
  245. Triple-clicking selects lines.
  246.  
  247. Clicking mouse-2 while selecting copies the region to the kill-ring.
  248. Clicking mouse-1 or mouse-3 kills the region.
  249.  
  250. This should be bound to a down-mouse event."
  251.   (interactive "e")
  252.   (mouse-set-point EVENT)
  253.   (setq mouse-sel-selection-type
  254.     (mouse-sel-determine-selection-type (event-click-count EVENT)))
  255.   (let ((object-bounds (bounds-of-thing-at-point mouse-sel-selection-type)))
  256.     (if object-bounds
  257.     (progn
  258.       (setq mark-active t)
  259.       (goto-char (car object-bounds))
  260.       (set-mark (cdr object-bounds)))
  261.       (deactivate-mark)))
  262.   (mouse-extend))
  263.  
  264. (defun mouse-extend (&optional EVENT)
  265.   "Extend region/selection using the mouse.
  266.  
  267. See documentation for mouse-select for more details.
  268.  
  269. This should be bound to a down-mouse event."
  270.   (interactive "e")
  271.   (if EVENT (select-window (posn-window (event-end EVENT))))
  272.   (let* ((min (if mark-active (region-beginning) (point)))
  273.      (max (if mark-active (region-end) (point)))
  274.      (orig-window (selected-window))
  275.      (orig-window-frame (window-frame orig-window))
  276.      (top (nth 1 (window-edges orig-window)))
  277.      (bottom (nth 3 (window-edges orig-window)))
  278.      (orig-cursor-type 
  279.       (cdr (assoc 'cursor-type (frame-parameters (selected-frame)))))
  280.      direction
  281.      event)
  282.  
  283.     ;; Inhibit normal region highlight
  284.     (setq mark-active nil)
  285.  
  286.     ;; Highlight region (forcing re-highlight)
  287.     (move-overlay mouse-drag-overlay min max (current-buffer))
  288.     (overlay-put mouse-drag-overlay 'face
  289.          (overlay-get mouse-drag-overlay 'face))
  290.  
  291.     ;; Bar cursor
  292.     (modify-frame-parameters (selected-frame) '((cursor-type . bar)))
  293.  
  294.     ;; Handle dragging
  295.     (unwind-protect
  296.     (progn 
  297.       (track-mouse
  298.         
  299.         (while (if EVENT        ; Use initial event
  300.                (prog1
  301.                (setq event EVENT)
  302.              (setq EVENT nil))
  303.              (setq event (read-event))
  304.              (and (consp event)
  305.               (memq (car event) '(mouse-movement switch-frame))))
  306.           
  307.           (let ((end (event-end event)))
  308.             
  309.         (cond
  310.              
  311.          ;; Ignore any movement outside the frame
  312.          ((eq (car-safe event) 'switch-frame) nil)
  313.          ((and (posn-window end)
  314.                (not (eq (window-frame (posn-window end))
  315.                 (window-frame orig-window)))) nil)
  316.              
  317.          ;; Different window, same frame
  318.          ((not (eq (posn-window end) orig-window))
  319.           (let ((end-row (cdr (cdr (mouse-position)))))
  320.             (cond
  321.              ((and end-row (not (bobp)) (< end-row top))
  322.               (mouse-scroll-subr (- end-row top)
  323.                      mouse-drag-overlay max))
  324.              ((and end-row (not (eobp)) (>= end-row bottom))
  325.               (mouse-scroll-subr (1+ (- end-row bottom))
  326.                      mouse-drag-overlay min))
  327.              )))
  328.  
  329.          ;; On the mode line
  330.          ((eq (posn-point end) 'mode-line)
  331.           (mouse-scroll-subr 1 mouse-drag-overlay min))
  332.  
  333.          ;; In original window
  334.          (t (goto-char (posn-point end)))
  335.  
  336.          )
  337.  
  338.         ;; Determine direction of drag
  339.         (cond
  340.          ((and (not direction) (not (eq min max)))
  341.           (setq direction (if (< (point) (/ (+ min max) 2)) -1 1)))
  342.          ((and (not (eq direction -1)) (<= (point) min))
  343.           (setq direction -1))
  344.          ((and (not (eq direction 1)) (>= (point) max))
  345.           (setq direction 1)))
  346.         
  347.         (if (not mouse-sel-selection-type) nil
  348.           
  349.           ;; If dragging forward, goal is next character
  350.           (if (and (eq direction 1) (not (eobp))) (forward-char 1))
  351.           
  352.           ;; Move to start/end of selected thing
  353.           (let ((goal (point))
  354.             last)
  355.             (goto-char (if (eq 1 direction) min max))
  356.             (condition-case nil
  357.             (progn
  358.               (while (> (* direction (- goal (point))) 0)
  359.                 (setq last (point))
  360.                 (forward-thing mouse-sel-selection-type 
  361.                        direction))
  362.               (let ((end (point)))
  363.                 (forward-thing mouse-sel-selection-type
  364.                        (- direction))
  365.                 (goto-char
  366.                  (if (> (* direction (- goal (point))) 0)
  367.                  end last))))
  368.               (error))))
  369.         
  370.         ;; Move overlay
  371.         (move-overlay mouse-drag-overlay
  372.                   (if (eq 1 direction) min (point))
  373.                   (if (eq -1 direction) max (point))
  374.                   (current-buffer))
  375.           
  376.         )))            ; end track-mouse
  377.  
  378.       (let ((overlay-start (overlay-start mouse-drag-overlay))
  379.         (overlay-end (overlay-end mouse-drag-overlay)))
  380.  
  381.         ;; Set region
  382.         (if (eq overlay-start overlay-end)
  383.         (deactivate-mark)
  384.           (if (and mouse-sel-leave-point-near-mouse (eq direction 1))
  385.           (progn
  386.             (set-mark overlay-start)
  387.             (goto-char overlay-end))
  388.         (set-mark overlay-end)
  389.         (goto-char overlay-start)))
  390.         
  391.         ;; Set selection
  392.         (if (and mark-active mouse-sel-set-selection-function)
  393.         (funcall mouse-sel-set-selection-function 
  394.              (buffer-substring overlay-start overlay-end)))
  395.           
  396.         ;; Handle copy/kill
  397.         (cond
  398.          ((eq (car-safe last-input-event) 'down-mouse-2)
  399.           (copy-region-as-kill overlay-start overlay-end)
  400.           (read-event) (read-event))
  401.          ((memq (car-safe last-input-event) '(down-mouse-1 down-mouse-3))
  402.           (kill-region overlay-start overlay-end)
  403.           (deactivate-mark)
  404.           (read-event) (read-event)))))
  405.  
  406.       ;; Restore cursor
  407.       (modify-frame-parameters (selected-frame) 
  408.                    (list (cons 'cursor-type orig-cursor-type)))
  409.       ;; Remove overlay
  410.       (or mouse-sel-retain-highlight
  411.       (delete-overlay mouse-drag-overlay)))))
  412.  
  413. (defun mouse-insert-selection (click)
  414.   "Insert the contents of the selection at mouse click."
  415.   (interactive "e")
  416.   (mouse-set-point click)
  417.   (deactivate-mark)
  418.   (if mouse-sel-get-selection-function
  419.       (insert (or (funcall mouse-sel-get-selection-function) ""))))
  420.  
  421. (defun mouse-sel-validate-selection ()
  422.   "Remove selection highlight if emacs no longer owns the primary selection."
  423.   (or (not mouse-sel-check-selection-function)
  424.       (funcall mouse-sel-check-selection-function)
  425.       (delete-overlay mouse-drag-overlay)))
  426.  
  427. (add-hook 'pre-command-hook 'mouse-sel-validate-selection)
  428.  
  429. ;;=== Key bindings ========================================================
  430.  
  431. (if (not mouse-sel-default-bindings) nil
  432.   
  433.   (global-unset-key [mouse-1])
  434.   (global-unset-key [drag-mouse-1])
  435.   (global-unset-key [mouse-3])
  436.   
  437.   (global-set-key [down-mouse-1]    'mouse-select)
  438.   (global-set-key [down-mouse-3]     'mouse-extend)
  439.   
  440.   (if (eq mouse-sel-default-bindings 'interprogram-cut-paste) nil
  441.     
  442.     (global-set-key [mouse-2]     'mouse-insert-selection)
  443.     (setq interprogram-cut-function nil
  444.       interprogram-paste-function nil))
  445.   
  446.   )
  447.  
  448. ;;=== END of mouse-sel.el =================================================
  449.